namespace Gsl
{
    using System;
    using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Sequential)]
    public class Matrix
    {
        int n1;
        int n2;
        int tda;
        double[] data;
        Block block;
        int owner;
                
        public Matrix(int n1, int n2)
        {
            if (n1 == 0)
                 throw new GslException(String.Format("Matrix: n1 must be greater than zero"), Error.Code.GSL_EINVAL);
            if (n2 == 0)
                 throw new GslException(String.Format("Matrix: n2 must be greater than zero"), Error.Code.GSL_EINVAL);
                 
            block = new Block(n1 * n2);
            this.data = block.Data;
            this.n1 = n1;
            this.n2 = n2;
            this.tda = n2;
            this.owner = 1;
        }
        
        public int NumRows
        {
            get { return n1; }
        }
        
        public int NumColumns
        {
            get { return n2; }
        }                
        
        public double this [int i, int j]
        {
            get { return data[i * tda + j]; }
            set {  data[i * tda + j] = value; }
        }
        
        public double[,] Data
        {
            get
            {
                double[,] tmp = new double[n1, n2];
                for (int i = 0; i < n1; i++)
                {
                    for (int j = 0; j < n2;)
                    {
                        tmp[i, j] = data[i * tda + j];
                    }
                }
                return tmp;
            }
        }
        
        public void SetAll(double x)
        {
            for (int i = 0; i < n1; i++)
            {
                for (int j = 0; j < n2; j++)
                    data[i * tda + j] = x;
            }
                
        }
        
        public void SetZero()
        {
            for (int i = 0; i < n1; i++)
            {
                for (int j = 0; j < n2; j++)
                    data[i * tda + j] = 0.0;
            }
        }
    }
}
